<?php


namespace App\Job;


use App\Config\QueueConfig;
use App\Http\Library\Pfs;
use App\Http\Library\Queue;
use App\Http\Model\Job\PfsHeadQueryClassBalanceModel;
use App\Http\Model\Job\PfsHeadQueryTransferBillModel;

class PfsHeadQueryClassBalanceJob
{

    /**
     * 3.10 班级拨付余额查询  机构端->银行
     *
     * @param array $data
     *
     * @throws \Exception
     */
    public static function main(array $data)
    {
        try {
            $result = [];
            $list = [];
            $classIdList = [];
            /*处理格式*/
            foreach ($data['class_list'] as $val) {
                $classIdList[] = $val['class_id'];
                $list[$val['class_id']] = $val['id'];
            }
            /*3.10 班级拨付余额查询  机构端->银行*/
            $result = Pfs::PfsHeadQueryClassBalance([
                /*机构编号，由招行分配*/
                'agencyId'    => $data['pfs_agencyid'],
                /*班级编号数组*/
                'classIdList' => $classIdList,
            ], [
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $data['pfs_encrypt_private'],
                /*招行-签名密钥*/
                'pfs_sign_private'    => $data['pfs_sign_private']
            ]);
            /*更新表记录*/
            PfsHeadQueryClassBalanceModel::save($result, $list);
        } catch (\Exception $exception) {
            log_arr([
                '[招行-班级拨付余额查询-异常]',
                'line'   => $exception->getLine(),
                'file'   => $exception->getFile(),
                'msg'    => $exception->getMessage(),
                'data'   => serialize($data),
                'result' => $result,
            ], 'pfs');
            throw new \Exception($exception->getMessage());
        }
    }


    /**
     * 队列任务空闲回调
     * 3.10 班级拨付余额查询  机构端->银行
     *
     * @throws \Exception
     */
    public static function call()
    {
        /*需要执行查询划拨的机构*/
        $list = PfsHeadQueryTransferBillModel::listPsfDw();
        foreach ($list as $val) {
            $offsetId = 0;
            $exist = true;
            while ($exist) {
                /*需要查询的班级ID*/
                $classList = PfsHeadQueryClassBalanceModel::classId($val->id, $offsetId);
                if (empty($classList)) {
                    $exist = false;
                    continue;
                }
                Queue::rpush([
                    /*招行&数仓-业务线分校配置信息主键ID*/
                    'pfs_dw_id'           => $val->id,
                    /*机构编号，由招行分配*/
                    'pfs_agencyid'        => $val->pfs_agencyid,
                    /*招行-签名密钥*/
                    'pfs_sign_private'    => $val->pfs_sign_private,
                    /*招行-加密密钥*/
                    'pfs_encrypt_private' => $val->pfs_encrypt_private,
                    /*班级编号数组*/
                    'class_list'          => $classList,
                ], QueueConfig::PFS_HEAD_QUERYCLASSBALANCE);
            }
        }
    }


}





/**
 * PfsHeadQueryClassBalanceJob.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/21 17:54:00   操作:创建
 **/
